package com.guodi.model.utils;

import com.guodi.map.entity.MapDivision;
import com.guodi.model.entity.ApiIdtIndicatorsDir;
import com.guodi.model.entity.ApiMdlModelDir;
import com.guodi.model.entity.IdtIndicatorsType;
import com.guodi.model.entity.MdlModelDir;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;


/**
* @描述: 递归查询children节点工具类
* @作者: 代鹏刚
* @日期: 2019/12/13 17:09
**/

public class RecursionUtils {

    /**
     * @描述: 模型分类构造树工具
     * @作者: 代鹏刚
     * @日期: 2020/4/3 11:40
     **/
    public static List<MdlModelDir> buildModelDirTree(List<MdlModelDir> dirList, String rootId){
        //定义返回结果集
        List<MdlModelDir> menuTree = new ArrayList<>();
        for (MdlModelDir mapProjectDir:dirList) {
            if(mapProjectDir.getParentId().equals(rootId)){
                menuTree.add(modelDirChildren(mapProjectDir,dirList));
            }
        }
        return menuTree;
    }
    public static MdlModelDir modelDirChildren(MdlModelDir treeNode, List<MdlModelDir> treeList){
        for (MdlModelDir it : treeList) {
            if(treeNode.getId().equals(it.getParentId())) {
                //递归查询子项
                modelDirChildren(it,treeList);
                if (treeNode.getChildren() == null) {
                    List<MdlModelDir> children = new ArrayList<>();
                    children.add(it);
                    treeNode.setChildren(children);
                }else {
                    List<MdlModelDir> children = treeNode.getChildren();
                    children.add(it);
                    treeNode.setChildren(children);
                }
            }
        }
        return treeNode;
    }

    public static List<ApiMdlModelDir> buildModelDirTree2(List<ApiMdlModelDir> dirList, String rootId){
        //定义返回结果集
        List<ApiMdlModelDir> menuTree = new ArrayList<>();
        for (ApiMdlModelDir mapProjectDir:dirList) {
            if(mapProjectDir.getParentId().equals(rootId)){
                menuTree.add(modelDirChildren2(mapProjectDir,dirList));
            }
        }
        return menuTree;
    }
    public static ApiMdlModelDir modelDirChildren2(ApiMdlModelDir treeNode, List<ApiMdlModelDir> treeList){
        for (ApiMdlModelDir it : treeList) {
            if(treeNode.getId().equals(it.getParentId())) {
                //递归查询子项
                modelDirChildren2(it,treeList);
                if (treeNode.getChildren() == null) {
                    List<ApiMdlModelDir> children = new ArrayList<>();
                    children.add(it);
                    treeNode.setChildren(children);
                }else {
                    List<ApiMdlModelDir> children = treeNode.getChildren();
                    children.add(it);
                    treeNode.setChildren(children);
                }
            }
        }
        return treeNode;
    }

    /**
     * @描述: 递归统计不同类型的指标数量，会统计底下所有子级的节点
     * @入参：currentNode 父节点，如果为null，则以当前最外层节点为父节点
     * @出参：currentNodeChildren 父节点的子节点列表
     * @作者: 陈宏成
     * @日期: 2021-04-08
     **/
    public static void countNum(ApiIdtIndicatorsDir currentNode, List<ApiIdtIndicatorsDir> currentNodeChildren){
        if (currentNode == null){
            for(ApiIdtIndicatorsDir currentNodeChild : currentNodeChildren) {
                countNum(currentNodeChild,currentNodeChild.getChildren());
            }
            return;
        }
        if (currentNodeChildren == null || currentNodeChildren.isEmpty()){
            if (currentNode.getType() == 1){
                currentNode.setBasicNum(0);
                currentNode.setRecommendNum(0);
                currentNode.setPlaceNum(0);
            }
            return;
        }
        // 设置数量
        int recommendNum = 0;
        int basicNum = 0;
        int placeNum = 0;
        // 遍历子级
        for(ApiIdtIndicatorsDir child : currentNodeChildren) {
            if (child.getType() == 2){
                if("basic".equalsIgnoreCase(child.getIdtType())){
                    basicNum++;
                }else if("recommend".equalsIgnoreCase(child.getIdtType())){
                    recommendNum++;
                }else if("place".equalsIgnoreCase(child.getIdtType())){
                    placeNum++;
                }
            } else if (child.getType() == 1) {
                // 如果是目录，递归处理
                countNum(child, child.getChildren());
                basicNum += child.getBasicNum() == null? 0 : child.getBasicNum();
                recommendNum += child.getRecommendNum() == null? 0 : child.getRecommendNum();
                placeNum += child.getPlaceNum() == null? 0 : child.getPlaceNum();
            }
        }
        currentNode.setBasicNum(basicNum);
        currentNode.setRecommendNum(recommendNum);
        currentNode.setPlaceNum(placeNum);
    }

    /**
     * @描述: 构造行政区划目录树-非以往方式
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2020/6/11 16:59
     **/
    public static List<MapDivision> buildMapDivision(List<MapDivision> dirList, String parentId){
        //定义返回结果集
        List<MapDivision> menuTree = new ArrayList<>();
        for (MapDivision mapDivision:dirList) {
            if(mapDivision.getParentCode().equals(parentId)){
                menuTree.add(mapDivisionChildren(mapDivision,dirList));
            }
        }
        return menuTree;
    }
    public static MapDivision mapDivisionChildren(MapDivision treeNode, List<MapDivision> treeList){
        for (MapDivision it : treeList) {
            if(treeNode.getCode().equals(it.getParentCode())) {
                //递归查询子项
                mapDivisionChildren(it,treeList);
                if (treeNode.getChildren() == null) {
                    List<MapDivision> children = new ArrayList<>();
                    children.add(it);
                    treeNode.setChildren(children);
                }else {
                    List<MapDivision> children = treeNode.getChildren();
                    children.add(it);
                    treeNode.setChildren(children);
                }
            }
        }
        return treeNode;
    }

    /**
    * @描述: 构造指标体系目录树
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2021/9/27 18:34
    **/
    public static List<IdtIndicatorsType> buildIdtIndicatorsTypeTree(List<IdtIndicatorsType> dirList){
        //定义返回结果集
        List<IdtIndicatorsType> menuTree = new ArrayList<>();
        for (IdtIndicatorsType idtIndicatorsType:dirList) {
            if(idtIndicatorsType.getParentId().equals("-1")){
                menuTree.add(functionDirChildren(idtIndicatorsType,dirList));
            }
        }
        return menuTree;
    }
    public static IdtIndicatorsType functionDirChildren(IdtIndicatorsType treeNode, List<IdtIndicatorsType> treeList){
        for (IdtIndicatorsType it : treeList) {
            if(treeNode.getId().equals(it.getParentId())) {
                //递归查询子项
                functionDirChildren(it,treeList);
                if(StringUtils.isBlank(it.getConfigType())){
                    it.setConfigType(treeNode.getConfigType());
                }
                if (treeNode.getChildren() == null) {
                    List<IdtIndicatorsType> children = new ArrayList<>();
                    children.add(it);
                    treeNode.setChildren(children);
                }else {
                    List<IdtIndicatorsType> children = treeNode.getChildren();
                    children.add(it);
                    treeNode.setChildren(children);
                }
            }
        }
        return treeNode;
    }

    /**
     * 递归创建树，针对 ApiIdtIndicatorsDir 类型
     * @入参:
     * @出参:
     * @作者: 郑文彬
     * @日期: 2020/4/20 17:30
     **/
    public static List<ApiIdtIndicatorsDir> buildApiIdtIndicatorsDirTree(String parentId, List<ApiIdtIndicatorsDir> apiIdtIndicatorsDirList) {
        List tree = new ArrayList<>();
        for (int i = 0; i < apiIdtIndicatorsDirList.size(); i++) {
            ApiIdtIndicatorsDir idtDirectory = apiIdtIndicatorsDirList.get(i);
            if (idtDirectory.getParentId().equals(parentId)) {
                tree.add(idtDirectory);
                List children = buildApiIdtIndicatorsDirTree(idtDirectory.getId(), apiIdtIndicatorsDirList);
                if (children != null && !children.isEmpty()) {
                    idtDirectory.setChildren(children);
                } else {
                    idtDirectory.setChildren(new ArrayList<>());
                }
            }
        }
        return tree;
    }
}
